package com.ydy.ewallet.common;


import com.ydy.ewallet.enums.ErrorEnums;

import java.io.Serializable;

/**
 * Rest返回结果
 *
 * @author Rlax
 */
public class RestResult implements Serializable {

    private String code;
    private String msg;

    private Object data;

    public RestResult() {

    }

    public RestResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public RestResult(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 操作成功，返回默认对象
     */
    public static RestResult ok() {
        return new RestResult(ErrorEnums.SUCCESS.getCode(), "操作成功");
    }

    /**
     * 操作失败
     *
     * @param errorEnums 错误枚举
     */
    public static RestResult fail(ErrorEnums errorEnums) {
        return new RestResult(errorEnums.getCode(), errorEnums.getMsg());
    }

    /**
     * 操作成功，返回指定对象
     *
     * @param t 数据对象
     * @return 返回成功消息
     */
    public static RestResult ok(Object t) {
        return new RestResult(ErrorEnums.SUCCESS.getCode(), "操作成功", t);
    }

    /**
     * 返回默认的错误信息提示
     *
     * @return 错误信息
     */
    public static RestResult fail() {
        return new RestResult(ErrorEnums.FAIL.getCode(), "操作失败");
    }

    /**
     * 返回指定的错误信息
     *
     * @param msg 错误提示信息
     * @return 失败消息
     */
    public static RestResult fail(String msg) {
        return new RestResult(ErrorEnums.FAIL.getCode(), msg);
    }

    /**
     * 返回指定的错误信息
     *
     * @param code 错误码
     * @param msg  错误提示信息
     * @return 失败消息
     */
    public static RestResult fail(String code, String msg) {
        return new RestResult(code, msg);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "RestResult{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

}
